﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class DeliverGoodsManagement_AddExpressBillTemplate : System.Web.UI.Page
{
    private DataTable ExpressBillTemplateTable
    {
        get
        {
            if (ViewState["ExpressBillTemplateTable"] == null)
            {
                ViewState["ExpressBillTemplateTable"] = CreateExpressBillTemplateTable();
            }
            return (DataTable)ViewState["ExpressBillTemplateTable"];
        }
        set
        {
            ViewState["ExpressBillTemplateTable"] = value;
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private DataTable CreateExpressBillTemplateTable()
    {
        DataTable tbl = new DataTable();

        DataColumn tblColumn = new DataColumn();
        tblColumn.DataType = System.Type.GetType("System.String");
        tblColumn.ColumnName = "Template_Name";
        tbl.Columns.Add(tblColumn);

        tblColumn = new DataColumn();
        tblColumn.DataType = System.Type.GetType("System.String");
        tblColumn.ColumnName = "Template_Description";
        tbl.Columns.Add(tblColumn);

        return tbl;
    }

    private void FillValueIntoExpressBillTemplateTable(DataTable tbl)
    {
        Label lblTemplateName;
        Label lblTemplateDescription;

        for (int i = 0; i < dlistExpressBillTemplate.Items.Count; i++)
        {
            lblTemplateName = (Label)dlistExpressBillTemplate.Items[i].FindControl("lblTemplateName");
            lblTemplateDescription = (Label)dlistExpressBillTemplate.Items[i].FindControl("lblTemplateDescription");

            tbl.Rows[i]["Template_Name"] = lblTemplateName.Text;
            tbl.Rows[i]["Template_Description"] = lblTemplateDescription.Text;
        }
    }

    public DataTable AddDataRowToDataTable(DataTable tbl, string templateName, string templateDescription)
    {
        DataRow newRow = tbl.NewRow();
        newRow["Template_Name"] = templateName;
        newRow["Template_Description"] = templateDescription;
        tbl.Rows.Add(newRow);
        return tbl;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTraner();
            BindDataListExpressBillTemplate();
        }
    }

    private void BindTraner()
    {
        List<Model.TRANER> traners = new BLL.TRANER().GetAllList();
        dropTraner.DataSource = traners;
        dropTraner.DataTextField = "TRANER_NAME";
        dropTraner.DataValueField = "TRANER_CODE";
        dropTraner.DataBind();
    }

    private void BindDataListExpressBillTemplate()
    {
        this.ExpressBillTemplateTable = new BLL.ExpressBillTemplate().GetByTranerId(dropTraner.SelectedValue).Tables[0];
        dlistExpressBillTemplate.DataSource = this.ExpressBillTemplateTable;
        dlistExpressBillTemplate.DataBind();
    }

    protected void dropTraner_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataListExpressBillTemplate();
    }
    protected void dlistExpressBillTemplate_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (!Check.CheckPermission("100100107", "00003"))
        {
            Share.Alert(this.Page, "无权操作！"); return;
        }
        if (e.CommandName.ToLower() == "del")
        {
            this.ExpressBillTemplateTable.Rows.RemoveAt(e.Item.ItemIndex);
        }
        dlistExpressBillTemplate.DataSource = this.ExpressBillTemplateTable;
        dlistExpressBillTemplate.DataBind();
    }
    protected void btnAddTemplate_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("100100107", "00001"))
        {
            Share.Alert(this.Page, "无权操作！"); return;
        }
        if (txtTemplateName.Text == "")
        {
            Share.Alert(this.Page, "请填写模板名称！"); return;
        }
        FillValueIntoExpressBillTemplateTable(this.ExpressBillTemplateTable);
        this.ExpressBillTemplateTable = AddDataRowToDataTable(this.ExpressBillTemplateTable, txtTemplateName.Text, txtTemplateDescription.Text);
        dlistExpressBillTemplate.DataSource = this.ExpressBillTemplateTable;
        dlistExpressBillTemplate.DataBind();
        txtTemplateName.Text = "";
        txtTemplateDescription.Text = "";
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("100100107", "00017"))
        {
            Share.Alert(this.Page, "无权操作！"); return;
        }
        StringBuilder sbSql = new StringBuilder();
        sbSql.Append("delete from ExpressBillTemplate where Traner_ID='");
        sbSql.Append(dropTraner.SelectedValue);
        sbSql.Append("'|");

        for (int i = 0; i < this.ExpressBillTemplateTable.Rows.Count; i++)
        {
            if (this.ExpressBillTemplateTable.Rows[i]["Template_id"].ToString() != "")
            {
                sbSql.Append("SET IDENTITY_INSERT [ExpressBillTemplate] ON|");
                sbSql.Append("INSERT INTO [ExpressBillTemplate]([Template_id],[Template_Name],[Template_Description],[Traner_ID]) VALUES('");
                sbSql.Append(this.ExpressBillTemplateTable.Rows[i]["Template_id"].ToString());
                sbSql.Append("','");
                sbSql.Append(this.ExpressBillTemplateTable.Rows[i]["Template_Name"].ToString());
                sbSql.Append("','");
                sbSql.Append(this.ExpressBillTemplateTable.Rows[i]["Template_Description"].ToString());
                sbSql.Append("','");
                sbSql.Append(dropTraner.SelectedValue);
                sbSql.Append("')|");
                sbSql.Append("SET IDENTITY_INSERT [ExpressBillTemplate] OFF|");
            }
            else
            {
                sbSql.Append("INSERT INTO [ExpressBillTemplate]([Template_Name],[Template_Description],[Traner_ID]) VALUES('");
                sbSql.Append(this.ExpressBillTemplateTable.Rows[i]["Template_Name"].ToString());
                sbSql.Append("','");
                sbSql.Append(this.ExpressBillTemplateTable.Rows[i]["Template_Description"].ToString());
                sbSql.Append("','");
                sbSql.Append(dropTraner.SelectedValue);
                sbSql.Append("')|");
            }

        }
        string stringSql = sbSql.ToString();
        char[] charArray = new char[] { '|' };
        string[] sqlCommandArray = stringSql.TrimEnd(charArray).Split(charArray);
        if (new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray))
        {
            Share.InsertLog("100100107", "00017");
            Share.Alert(this.Page,"修改模板成功");
        }
        else
        {
            Share.Alert(this.Page, "修改模板失败！");
        }
    }
    protected void dlistExpressBillTemplate_EditCommand(object source, DataListCommandEventArgs e)
    {
        dlistExpressBillTemplate.EditItemIndex = e.Item.ItemIndex;
        BindDataListExpressBillTemplate();
    }
    protected void dlistExpressBillTemplate_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dlistExpressBillTemplate.EditItemIndex = -1;
        BindDataListExpressBillTemplate();
    }
    protected void dlistExpressBillTemplate_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        TextBox txtTemplateName = (TextBox)e.Item.FindControl("txtTemplateName");
        Model.ExpressBillTemplate template = new BLL.ExpressBillTemplate().GetModel(Convert.ToInt32(e.CommandArgument));
        if (template != null)
        {
            template.Template_Name = txtTemplateName.Text.Trim();
            if (new BLL.ExpressBillTemplate().Update(template))
            {
                dlistExpressBillTemplate.EditItemIndex = -1;
                BindDataListExpressBillTemplate();
            }
            else
            {
                Share.Alert(this.Page,"更新失败");
                //dlistExpressBillTemplate.EditItemIndex = -1;
                //BindDataListExpressBillTemplate();
            }
        }
    }
}
